package com.yiqi.zhiyuan.feature.mine.model;

import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.yiqi.zhiyuan.entity.dto.FeedbackDTO;
import com.yiqi.zhiyuan.entity.vo.FileVO;
import com.yiqi.zhiyuan.feature.mine.contract.FeedbackContract;
import com.yiqi.zhiyuan.feature.mine.dependencies.feedback.DaggerFeedbackModelComponent;
import com.yiqi.zhiyuan.framework.api.ApiLib;
import com.yiqi.zhiyuan.framework.api.UserApi;

import java.io.File;

import javax.inject.Inject;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * @ProjectName: ZhiYuan
 * @Description: 注册model
 * @Author: lijuwen
 * @CreateDate: 2019/3/13 13:07
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/3/13 13:07
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class FeedbackModel implements FeedbackContract.Model {
    @Inject
    UserApi api;

    public FeedbackModel() {
        DaggerFeedbackModelComponent.builder().apiComponent(ApiLib.apiComponent()).build().inject(this);
    }


    @Override
    public Observable<FileVO> doUploadImage(String imageUrl) {
        File file = new File(imageUrl);
        RequestBody requestFile = RequestBody.create(MediaType.parse("application/otcet-stream"), file);
        MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", file.getName(), requestFile);
        return api.uploadFile(filePart).compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }

    @Override
    public Observable<String> doFeedback(FeedbackDTO feedbackDTO) {
        return api.insertDriverFeedback(feedbackDTO).compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }

    @Override
    public Observable<FileVO> doUpdateAvatar(String avatarUrl) {
        File file = new File(avatarUrl);
        RequestBody requestFile = RequestBody.create(MediaType.parse("application/otcet-stream"), file);
        MultipartBody.Part filePart = MultipartBody.Part.createFormData("images", file.getName(), requestFile);
        return api.driverHeadPhotoImags(filePart).compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }
}

